package cn.mofuxiong.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;

import cn.common.dao.BaseDao;

import cn.mofuxiong.Model.login_register;



public class UserDao extends BaseDao{
	Connection conn = null;
	 Statement stmt = null;
	 PreparedStatement pstmt=null;
	 ResultSet rs=null;
	//客服,管理员登录
	public ResultSet login(String a_userId,String passwd,int identify){
		   String sql = null;
           Connection conn = null;
           conn = this.getConn();
           login_register pojo = new login_register();
           
		try {
			
			sql = "SELECT * FROM admin where a_userId=? and a_passwd=? and a_status=?";		
            PreparedStatement ps = conn.prepareStatement(sql);
			
			ps.setObject(1, a_userId);
			
			ps.setObject(2,passwd);
			
			ps.setObject(3, identify);
			
			return ps.executeQuery();
			
		} catch(Exception e) {
			e.printStackTrace();
		}		
		return null;
	}
	
	
	//客服注册
	public int register(String name,String passwd){
		Connection conn = null;
		String sql = "insert into admin(a_userId,a_passwd,a_status) values(?,?,0)";
		conn = this.getConn();
		System.out.println(name);
		try{
			
			    pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,name);
				pstmt.setString(2, passwd);
			
				
				int num = pstmt.executeUpdate();
				if(num==1){
					this.closeAll();
					return 1;
				}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
			return 0;
		
	     }
	
	
	
	@SuppressWarnings("unused")
	public LinkedHashMap select(String UserId){ //异步校验
		boolean flag=false;
		LinkedHashMap map=null;
	
		login_register admin=new login_register();
		try{
			conn = this.getConn();
			String sql="select * from admin where a_userId=?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setObject(1, UserId);
		    ResultSet rs=pstmt.executeQuery();
		
		   
		    ResultSetMetaData rsmd=rs.getMetaData();
		    int colunmn=rsmd.getColumnCount();
		    while(rs!=null&&rs.next())
		    {
				  map=new LinkedHashMap(colunmn);			    
			      for(int i=1;i<=colunmn;i++)
			      {       //把结果集的数一个一个取出来			    				    	 
			    	  map.put(rsmd.getColumnName(i), rs.getObject(i));		    	 
			      }
			 }
			
			if(rs!=null)
			{
				flag=true;
		    }
			rs.close();
			
			if(pstmt!=null)
				pstmt.close();
			this.closeConn();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		 return map;
		
	
		
	    }
	}


